package com.night.administration.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.net.UnknownHostException;

/**
 * @ClassName: RedisConfiguration
 * @Description: //创建一个自己的configuration,为了改变redisTemplate的序列化器
 * @author: LZJ
 * @date: 2022/9/18-13:41
 * @version: v1.0
 */
@Configuration
public class RedisConfiguration {

    @Bean
//    @ConditionalOnMissingBean(name = "redisTemplate") 这个注解的意思是没有这个bean则会创建这个bean，所以这里直接去掉
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //修改默认序列化器
//        template.setDefaultSerializer(RedisSerializer.json());
        //分别设置k和V的序列化格式 K--String   V--JSON
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.json());
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.json());
        return template;
    }
}
